package com.bw.repair

import androidx.lifecycle.MutableLiveData
import com.bw.lib_base.db.RepairDao
import com.bw.lib_base.mvvm.BaseViewModel
import com.bw.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import retrofit2.http.Body
import javax.inject.Inject

@HiltViewModel
class RepairViewModel @Inject constructor(val apiService: ApiService, val repairDao: RepairDao): BaseViewModel() {
    val repairData = MutableLiveData<RepairBean>()

    fun getRepair(){
        httpRequest({apiService.repair()}, repairData::postValue)
    }

    val repairByNumData = MutableLiveData<RepairBeanItem>()
    fun getRepairByNum(num:String){
        httpRequest({apiService.getRepairByNum(num)},repairByNumData::postValue)
    }

    val repairByNameData = MutableLiveData<RepairBean>()
    fun getRepairByName(repairName:String){
        httpRequest({apiService.getRepairByName(repairName)},repairByNameData::postValue)
    }

    val repairByIdData = MutableLiveData<RepairBeanItem>()
    fun getDetail(id: Int){
        httpRequest({apiService.getDetail(id)}, repairByIdData::postValue)
    }

    val updateData = MutableLiveData<RepairBeanItem>()
    fun updateRepair(id: Int, body: RepairBeanItem){
        httpRequest({apiService.updateRepair(id,body)}, updateData::postValue)
    }

    val addData = MutableLiveData<RepairBeanItem>()

    fun addRepair(body: RepairBeanItem){
        httpRequest({apiService.addRepair(body)},addData::postValue)
    }

    val deleteRepairData = MutableLiveData<Boolean>()
    fun deleteRepair(id: Int){
        httpRequest({apiService.deleteRepair(id)},deleteRepairData::postValue)
    }


    //room相关操作
    val addRepairRoomData = MutableLiveData<Long>()
    fun addRepairRoom(repairBeanItem: RepairBeanItem){
        roomRequest({repairDao.insertRepair(repairBeanItem)},addRepairRoomData::postValue)
    }

    val queryRepairData = MutableLiveData<List<RepairBeanItem>>()
    fun queryAll(){
        roomRequest({repairDao.queryAll()},queryRepairData::postValue)
    }

    val queryRepairByOrderData = MutableLiveData<RepairBeanItem>()
    fun queryByOrder(orderNumber: String){
        roomRequest({repairDao.queryByOrder(orderNumber)},queryRepairByOrderData::postValue)
    }

    val addAllRoomData = MutableLiveData<List<Long>>()
    fun addAllRoom(repairBean: List<RepairBeanItem>){
        roomRequest({repairDao.insertAll(repairBean)},addAllRoomData::postValue)
    }

    val deleteRoom = MutableLiveData<Unit>()
    fun delete(repairBeanItem: RepairBeanItem){
        roomRequest({repairDao.delete(repairBeanItem)}, deleteRoom::postValue)
    }

    val repairByIdRoomData = MutableLiveData<RepairBeanItem>()
    fun getRepairByIdRoom(id: Int){
        roomRequest({repairDao.getRepairById(id)}, repairByIdRoomData::postValue)
    }
}